package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public Result findAll(String username) {
        try {
            //根据用户名查询数据库
            com.itheima.pojo.User user1 = menuDao.findByusername(username);
            //根据用户id查询角色并封装菜单信息
            Set<Role> roleSet = menuDao.findAll(user1.getId());
            //创建一个set集合用来存储没有自关联的菜单
            List<Menu> menus = new ArrayList<>();
            for (Role role : roleSet) {
                //根据角色id查询菜单
                List<Menu> menuSet = menuDao.findByMenu(role.getId());
                for (Menu menu : menuSet) {
                    List<Menu> list = new ArrayList<>();
                    if (menu.getParentMenuId()==null){
                        menu.setTitle(menu.getName());
                        //如果是主菜单 再次进行遍历查询子菜单
                        for (Menu menu1 : menuSet) {
                            if (menu.getId()==menu1.getParentMenuId()){
                                menu1.setTitle(menu1.getName());
                                //说明是子菜单
                                list.add(menu1);
                            }
                        }
                        menu.setChildren(list);
                        menus.add(menu);
                    }
                }
            }
            return new Result(true,"查询菜单成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }

    @Override
    public PageResult selectAllByName(QueryPageBean queryPageBean) {
        try {
            //获取参数名称
            String queryString = queryPageBean.getQueryString();
            //使用分页插件查询
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            //查询出所有主菜单
            Page<Menu> menuPage = menuDao.selectAllByName(queryString);
            List<Menu> result = menuPage.getResult();
            List<Menu> menus = new ArrayList<>();
            for (Menu menu : result) {
                if (menu.getParentMenuId()==null||menu.getParentMenuId()==0){
                    //根据id获取对应的子菜单
                    List<Menu> menuList = menuDao.findById(menu.getId());
                    menu.setChildren(menuList);
                    menus.add(menu);
                }
            }
            return new PageResult(menuPage.getTotal(),menus);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result add( Menu menu) {
        try {
            menuDao.add(menu);
            return new Result(true,"添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单失败");
        }
    }

    @Override
    public Result update(Menu menu) {
        try {
            menuDao.update(menu);
            return new Result(true,"菜单修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单修改失败");
        }
    }

    @Override
    public Result delete(int id) {
        try {
            //先查询用户是否关联该菜单
            Integer count = menuDao.findRoleById(id);
            if (count>0){
                //先删除中间表
                menuDao.deleteRoleAndMenu(id);
            }
            //删除菜单
            menuDao.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    @Override
    public Result selectAll() {
        try {
            List<Menu> menuList = menuDao.selectAll();
            //存入所有主菜单
            List<Menu> menus = new ArrayList<>();
            for (Menu menu : menuList) {
                if (menu.getParentMenuId()==null){
                    menus.add(menu);
                }
            }
            return new Result(true,"查询所有菜单成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有菜单失败");
        }
    }

    @Override
    public Result findByPath(int path) {
        try {
            //根据路径查询
            Menu menu = menuDao.findByPath(path);
            //根据id查询所有
            List<Menu> menuList = menuDao.findByMid(menu.getId());
            int last = 1;
            for (Menu mm : menuList) {
                //获取所有子菜单的路径
                String mmPath = mm.getPath();
                //截取路径
                int i = mmPath.lastIndexOf("-");
                int parseInt = Integer.parseInt(mmPath.substring(i+1));
                last = parseInt>=last?parseInt+1:last;
            }
            Menu me = new Menu();
            me.setPath("/"+path+"-"+last);
            me.setPriority(last);
            me.setId(menu.getId());
            return new Result(true,"查询成功",me);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @Override
    public Result findAllMenu() {
        try {
            List<Menu> menuList = menuDao.selectAll();
            return new Result(true,"查询成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

}
